module ApplicationHelper
  require 'user_context.rb'
  def signed_in?
    user_context = session[:user_context]
    if user_context.nil? || user_context.user.nil?
      false
    else
      true
    end    
  end
  
  def user
    user_context = session[:user_context]
    user = user_context.user    
    return user    
  end  
  
  def userContext
    user_context = session[:user_context]
    if user_context.nil?
      return nil
    else
      user_context
    end
  end
  
  def add_merchant_to_user_context(merchant)
    user_context = session[:user_context]
    user_context.merchant = merchant
    session['user_context'] = user_context
  end
  
  def create_user_context(user)
    user_context = UserContext.new
    user_context.user = user
    session['user_context'] = user_context
  end
  
  def is_merchant_user
    user_context = session[:user_context]
    if !user_context.merchant.nil?
      return true
    else
      return false
    end
  end
  
  def get_current_merchant
    user_context = session[:user_context]
    if user_context.nil? || user_context.user.nil? || user_context.merchant.nil?
      return nil
    else
      return user_context.merchant
    end 
  end
  
  def does_user_merchant_match(merchant_id)
    user_context = session[:user_context]
    if user_context.nil? || user_context.user.nil? || user_context.merchant.nil?
      return nil
    else
      if user_context.user.id == user_context.merchant.user.id
        return user_context.merchant 
      else      
        return nil
      end
    end 
  end
  
  def sanitize_filename(file_name)
    just_filename = File.basename(file_name)
    just_filename.sub(/[^\w\.\-]/,'_')
    return just_filename
  end
  
end
